﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HZY.App.Controllers
{
    using HZY.Services.Sys;
    using HZY.EFCore.Entitys.Sys;
    using HZY.EFCore.Entitys.Base;
    using Microsoft.EntityFrameworkCore;
    using HZY.EFCore;

    public class UserController : ApiBaseController
    {
        private readonly UserService userService;
        public UserController(UserService _userService) => this.userService = _userService;

        [HttpGet("gets")]
        public async Task<TableViewModel> FindAll()
        {
            //return await this.userService.db.FindAllAsync();

            return await this.userService.FindListAsync(1, 20, null);
        }

        [HttpGet("get/{Id}")]
        public async Task<Sys_User> FindById(Guid Id) => await this.userService.db.FindByIdAsync(Id);

        [HttpGet("add")]
        public async Task<Guid> Add()
        {
            var now = DateTime.Now;

            var model = new Sys_User();

            //this.userService.db.Context.Database.

            await this.userService.db.UnitOfWork.CommitAsync(async () =>
            {
                model.User_Name = "HZY." + now;
                model.User_LoginName = "HZY";
                model.User_Pwd = "123456";
                model.User_Email = "123123@qq.com";

                await this.userService.db.InsertAsync(model);

                //throw new Exception("哈哈 我错了.触发事务回滚");

            });

            return (Guid)model.User_ID;
        }



    }
}